<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/logout.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="myblog_list.html">我的主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="javascript:logout()">注销</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <!-- img/avatar.png -->
                <img id="photo" referrer="no-referrer|origin|unsafe-url" src="./img/avatar1.png" class="avtar" onerror="this.src='./img/avatar1.png'">

                <h3 id = "username"></h3>
                <a href="https://gitee.com/dashboard">gitee 地址</a>
                <div class="counter">
                    <span>文章</span>
                </div>
                <div class="counter">
                    <span id="artcount"></span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div id="artListDiv" class="container-right" >
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            
        </div>
    </div>
    <script>
        // 初始化方法
        function init(){
            //location.href = location.href;
            jQuery.ajax({
                url:"/art/mylist",
                type:"GET",
                data:{},
                success:function(res){
                    if (res.code == 200 && res.data != null){
                        // 请求成功
                        var createHtml = "";
                        var user = res.data.user;
                        var artList = res.data.list;
                        if (artList == null || artList.length == 0){
                            // 未发表文章
                            createHtml += "<h3 style='margin-left:20px;margin-top:20px'>暂无文章，请先"+
                                "<a href='blog_add.html'>添加</a></h3>";
                        }else{
                            for ( var i=0;i<artList.length;i++ ){
                                var art = artList[i];
                                createHtml += '<div class="blog">';
                                createHtml += '<div class="title">' + art.title + '</div>';
                                createHtml += '<div class="date">' + art.createtime + '</div>';
                                createHtml += '<div class="desc">'
                                createHtml += art.content;
                                createHtml += '</div>';
                                createHtml += '<a href="blog_content.html?aid=' + 
                                    art.id + '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;';
                                createHtml += '<a href="blog_edit.html?aid=' + 
                                    art.id + '"class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;';
                                createHtml += '<a href="javascript:del(' + art.id + ')" class="detail">删除 &gt;&gt;</a>'
                                createHtml += '</div>';
                            }
                        }
                        jQuery("#artListDiv").html(createHtml);

                        // 现在开始部署左侧的用户数据
                        if (user != null){
                            var userId = user.id;
                            console.dir(user)
                            // 给用户对象设置值
                            // jQuery("#photo").attr("onerror", "./img/avatar1.png");
                            jQuery("#photo").attr("src", "/img/photo/" + userId +".jpg" + "?time=" + new Date().getTime());
                            // jQuery("#photo").attr("src", "D:\\" + userId);
                            jQuery("#username").html(user.username);
                            jQuery("#artcount").html(user.artCount); // 用户发布的总文章数
                            // initEdit();
                        }else{
                            alert("查询失败1" + res.msg);
                        }
                    }else{
                        alert("操作失败" + res.msg);
                    }
                }
            });
        }
        init();
        // 根据文章 ID 进行删除操作
        function del(aid){
            // 1. 参数校验
            if (aid == "" || aid <= 0){
                alert("参数错误");
                return false;
            }
            // 2. 将数据返回给后端进行删除操作
            jQuery.ajax({
                url:"/art/del",
                type:"POST",
                data:{
                    "aid":aid
                },
                success:function(res){
                    // 3. 将结果展示给用户
                    if (res.code == 200 && res.data == 1){
                        alert("删除成功");
                        // 刷新当前页面
                        location.href = location.href;
                    }else{
                        // 删除失败
                        alert("操作失败" + res.msg);
                    }

                }
            });

        }

        // 点击头像触发事件
        jQuery("#photo").click(function(){
            location.href = "Photo_add.html";
            // <a href=""></a>
        });
    </script>
</body>

</html>